package com.cn.wanxi.controller;


import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.entity.ProductEntity;
import com.cn.wanxi.entity.SortEntity;
import com.cn.wanxi.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/back/product")
public class ProductController {

    @Autowired
    private IProductService iProductService;

    @RequestMapping("/findAll")
    @ResponseBody
    public void findAll(ProductEntity productEntity, HttpServletResponse resp) throws IOException {

        List<ProductEntity> list = iProductService.findAll(productEntity);
        int count = iProductService.getCount(productEntity);//得到分页总条数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }

    @RequestMapping("/del")
    public void del(ProductEntity productEntity, HttpServletResponse resp) throws IOException {

        String result = iProductService.del(productEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);
    }

    @RequestMapping("/findById")
    public void findById(ProductEntity productEntity, HttpServletResponse resp) throws IOException {

        ProductEntity model = iProductService.findById(productEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }

    @RequestMapping("/edit")
    public void edit(ProductEntity productEntity, HttpServletRequest req,HttpServletResponse resp) throws IOException {

        String result = iProductService.edit(productEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }

    @RequestMapping("/add")
    public void add(ProductEntity productEntity, HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String result = iProductService.add(productEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
}
